package com.test.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;

public class CreateKaptcha {
    // 验证码字符集
    private static final char[] chars = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    private static Map<String,String> map = new HashMap<String,String>();
    private static List<String> list= new ArrayList<String>();
    static{
        map.put("34-1+9","42");
        map.put("31+8=?","39");
        map.put("62-2+6","66");
        map.put("22*4=?","88");
        map.put("111*8=?","888");
        map.put("94-8=?", "86");
        map.put("光头强职业", "伐木工");

        Set<String> set=map.keySet();
        for(String s:set){
            list.add(s);
        }
        set=null;
    }
    // 字符数量
    private static final int SIZE = 4;
    // 干扰线数量
    private static final int LINES = 5;
    // 宽度
    private static final int WIDTH = 130;
    // 高度
    private static final int HEIGHT = 35;
    // 字体大小
    private static final int FONT_SIZE =15;
    /**
     * 生成随机验证码及图片
     */
    public static Object[] createImage() {
        StringBuffer sb = new StringBuffer();
        // 1.创建空白图片
        BufferedImage image = new BufferedImage(
                WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        // 2.获取图片画笔
        Graphics graphic = image.getGraphics();
        // 3.设置画笔颜色
        graphic.setColor(Color.LIGHT_GRAY);
        // 4.绘制矩形背景
        graphic.fillRect(0, 0, WIDTH, HEIGHT);
        // 5.画随机字符
        Random ran = new Random();
        if(ran.nextInt(2)==0){
            for (int i = 0; i <SIZE; i++) {
                // 取随机字符索引
                int n = ran.nextInt(chars.length);
                // 设置随机颜色
                graphic.setColor(getRandomColor());
                // 设置字体大小
                int size=(ran.nextInt(15)+FONT_SIZE);
                graphic.setFont(new Font(
                        null, Font.ITALIC,size ));
                // 画字符
                graphic.drawString(
                        chars[n] + "", i * WIDTH /SIZE, HEIGHT/2+5);
                // 记录字符
                sb.append(chars[n]);
            }
        }else{
            String key = list.get(ran.nextInt(list.size()));
            char[] cha = key.toCharArray();
            graphic.setColor(new Color(0,0,0));
            for(int i=0;i<cha.length;i++){
                int size = WIDTH/cha.length-2;
                graphic.setFont(new Font(null,Font.ITALIC+1,size));
                graphic.drawString(cha[i]+"", i * WIDTH/cha.length, HEIGHT/2+5);
            }
            char[] value=map.get(key).toCharArray();
            for(int i=0;i<value.length;i++){
                sb.append(value[i]);
            }
        }
        // 6.画干扰线
        for (int i = 0; i < LINES; i++) {
            // 设置随机颜色
            graphic.setColor(getRandomColor());
            // 随机画线
            graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
                    ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
        }
        // 7.返回验证码和图片
        return new Object[]{sb.toString(), image};
    }

    /**
     * 随机取色
     */
    public static Color getRandomColor() {
        Random ran = new Random();
        Color color = new Color(ran.nextInt(256),
                ran.nextInt(256), ran.nextInt(256));
        return color;
    }
}
